fix: jsonata $base64decode/$base64encode in Web Worker#2275
fix: jsonata $base64decode/$base64encode in Web Worker#2275min23asdw wants to merge 4 commits intogchq:masterfrom
Conversation
The jsonata library checks `typeof window !== 'undefined'` to decide whether to use browser `atob`/`btoa` or Node.js `Buffer.from` for base64 operations. Since CyberChef runs operations in a Web Worker where `window` is undefined, it falls back to `global.Buffer` which also does not exist, causing "Cannot read properties of undefined (reading 'from')". Fix by registering custom base64 functions on the expression that use `atob`/`btoa` directly, which are available in both browser and Web Worker scopes. Closes gchq#2063
GCHQDeveloper581
left a comment
There was a problem hiding this comment.
Could you also add a (single) test in tests/browser/02_ops.js to test this in a browser context.
|
Add browser test @GCHQDeveloper581 I do it right?. this |
|
Looks good apart from the linting error, though (because of that) I haven't seen whether the test passes or fails yet! Also - on PR #2270 you indicated that you'd used the assistance of AI tools. Is this also the case for your other PRs (including this one?) |
|
I use AI to add Add browser test (antigravity) and i forgot to run lint check on last commit .... i feel bad . my bad sorry |
|
Fix: prove that. It Look good on http://localhost/ |
C85297
left a comment
There was a problem hiding this comment.
LGTM from a code standpoint but from an AI standpoint we will have to confer
|
I won't be sad if the fix doesn't come from my PR ..... I just want to see it fixed.
I then searched online to verify whether those suggestions were correct, |



Summary
Fixes
$base64decodeand$base64encodeJSONata functions which throwCannot read properties of undefined (reading 'from')when used in the browser.Root cause: The jsonata library checks
typeof window !== 'undefined'to choose between browseratob/btoaand Node.jsBuffer.from. CyberChef runs operations in a Web Worker wherewindowis undefined, so it falls back toglobal.Buffer— which also doesn't exist in workers.Fix: Register custom
$base64decode/$base64encodefunctions on the expression object usingatob()/btoa(), which are available in both browser window and Web Worker scopes.Closes #2063
Changes
src/core/operations/Jsonata.mjsevaluate()tests/operations/tests/Jsonata.mjs$base64decodeand$base64encodeTest plan
npx grunt lint— passesnpm test— 1893/1893 passing (Node 18)▎ This pull request was created with the assistance of Claude and Antigravity
AI usage: I used AI (Claude and Antigravity) to suggest ideas on where it might be broken and to help write the tests.
I then searched online to verify whether those suggestions were correct,
confirmed it myself,
and then actually ran the tests locally.